package com.wswy.wyjk.ui.practiceNew.Adapter;

import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.my.httpapi.api.baseUtils.LogUtils;
import com.wswy.wyjk.AppContext;
import com.wswy.wyjk.R;
import com.wswy.wyjk.model.PracticeData;
import com.wswy.wyjk.ui.drawable.CenterImageSpan;
import com.wswy.wyjk.ui.drawable.QuestionTypeDrawable;
import com.wswy.wyjk.ui.main.model.PracticeModel;
import com.wswy.wyjk.ui.practiceNew.event.PracticeEvent;
import com.wswy.wyjk.ui.widget.OptionImageView;
import com.wswy.wyjk.utils.GlideUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class PracticeAdapter extends BaseQuickAdapter<PracticeData, BaseViewHolder> {

    private int mode = 1;//1-答题  2-背题
    private int answer;
    private int type;

    public PracticeAdapter(int type) {
        super(R.layout.layout_practice_item);
        this.type = type;
    }

    @Override
    protected void convert(BaseViewHolder helper, PracticeData practiceData) {
        answer = -1;
        Map<Integer, Boolean> map = new HashMap<>();
        helper.setIsRecyclable(false);
        TextView tv_question = helper.getView(R.id.tv_question);
        QuestionTypeDrawable imageSpan;
        if (practiceData.getOptionType() == PracticeData.TYPE_JUDGE) {
            imageSpan = new QuestionTypeDrawable(mContext.getString(R.string.judge_question));
        } else if (practiceData.getOptionType() == PracticeData.TYPE_RADIO) {
            imageSpan = new QuestionTypeDrawable(mContext.getString(R.string.radio_question));
        } else {
            imageSpan = new QuestionTypeDrawable(mContext.getString(R.string.multiple_question));
        }
        SpannableString spanString = new SpannableString("." + practiceData.getQuestion());
        spanString.setSpan(new CenterImageSpan(imageSpan), 0, 1, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
        tv_question.setText(spanString);

        helper.setText(R.id.tv_option_a, practiceData.getOptionA());
        helper.setText(R.id.tv_option_b, practiceData.getOptionB());
        helper.setText(R.id.tv_option_c, practiceData.getOptionC());
        helper.setText(R.id.tv_option_d, practiceData.getOptionD());

        isShow(practiceData.getOptionC(), helper.getView(R.id.rl_option_c));
        isShow(practiceData.getOptionD(), helper.getView(R.id.rl_option_d));

        //题目图片
        if (TextUtils.isEmpty(practiceData.getMediaUrl())) {
            helper.setVisible(R.id.img_question, false);
            helper.setVisible(R.id.video_view, false);
        } else {
            if ("mp4".equals(practiceData.getMediaUrl().substring(practiceData.getMediaUrl().length() - 3))) {
                helper.setVisible(R.id.img_question, false);
                helper.setVisible(R.id.video_view, true);
                VideoView mVideoView = helper.getView(R.id.video_view);
                mVideoView.setVideoPath(practiceData.getMediaUrl());
                MediaController mc = new MediaController(mContext);
                mc.setVisibility(View.INVISIBLE);
                mVideoView.setMediaController(mc);
                mVideoView.seekTo(0);
                mVideoView.requestFocus();
                mVideoView.start();
            } else {
                helper.setVisible(R.id.img_question, true);
                helper.setVisible(R.id.video_view, false);
                GlideUtils.load(mContext, practiceData.getMediaUrl(), helper.getView(R.id.img_question),
                        R.drawable.image_timu_default);
            }
        }

        helper.getView(R.id.rl_option_a).setOnClickListener(view -> {
            if (practiceData.getOptionType() == 3) {
                if (map.containsKey(1) && map.get(1)) {
                    map.put(1, false);
                    helper.getView(R.id.rl_option_a).setBackgroundColor(mContext.getResources().getColor(R.color.white));
                } else {
                    map.put(1, true);
                    helper.getView(R.id.rl_option_a).setBackgroundColor(mContext.getResources().getColor(R.color.textColorTertiary));
                }
                return;
            }
            answer(practiceData.getAnswer() == 1, helper.getView(R.id.img_option_a), helper.getView(R.id.tv_option_a), helper, practiceData, 1,helper.getAdapterPosition());
        });
        helper.getView(R.id.rl_option_b).setOnClickListener(view -> {
            if (practiceData.getOptionType() == 3) {
                if (map.containsKey(2) && map.get(2)) {
                    map.put(2, false);
                    helper.getView(R.id.rl_option_b).setBackgroundColor(mContext.getResources().getColor(R.color.white));
                } else {
                    map.put(2, true);
                    helper.getView(R.id.rl_option_b).setBackgroundColor(mContext.getResources().getColor(R.color.textColorTertiary));
                }
                return;
            }
            answer(practiceData.getAnswer() == 2, helper.getView(R.id.img_option_b), helper.getView(R.id.tv_option_b), helper, practiceData, 2,helper.getAdapterPosition());
        });
        helper.getView(R.id.rl_option_c).setOnClickListener(view -> {
            if (practiceData.getOptionType() == 3) {
                if (map.containsKey(3) && map.get(3)) {
                    map.put(3, false);
                    helper.getView(R.id.rl_option_c).setBackgroundColor(mContext.getResources().getColor(R.color.white));
                } else {
                    map.put(3, true);
                    helper.getView(R.id.rl_option_c).setBackgroundColor(mContext.getResources().getColor(R.color.textColorTertiary));
                }
                return;
            }
            answer(practiceData.getAnswer() == 3, helper.getView(R.id.img_option_c), helper.getView(R.id.tv_option_c), helper, practiceData, 3,helper.getAdapterPosition());
        });
        helper.getView(R.id.rl_option_d).setOnClickListener(view -> {
            if (practiceData.getOptionType() == 3) {
                if (map.containsKey(4) && map.get(4)) {
                    map.put(4, false);
                    helper.getView(R.id.rl_option_d).setBackgroundColor(mContext.getResources().getColor(R.color.white));
                } else {
                    map.put(4, true);
                    helper.getView(R.id.rl_option_d).setBackgroundColor(mContext.getResources().getColor(R.color.textColorTertiary));
                }
                return;
            }
            answer(practiceData.getAnswer() == 4, helper.getView(R.id.img_option_d), helper.getView(R.id.tv_option_d), helper, practiceData, 4,helper.getAdapterPosition());
        });

        helper.getView(R.id.tv_submit).setOnClickListener(view -> {
            answerDouble(helper, practiceData, map, true,helper.getAdapterPosition());
        });

        helper.setVisible(R.id.tv_submit, false);
        if (mode == 1) {
            helper.setVisible(R.id.tv_difficulty, false);
            helper.setVisible(R.id.ll_practice_explain, false);
            helper.getView(R.id.rl_option_a).setEnabled(true);
            helper.getView(R.id.rl_option_b).setEnabled(true);
            helper.getView(R.id.rl_option_c).setEnabled(true);
            helper.getView(R.id.rl_option_d).setEnabled(true);
            if (practiceData.getOptionType() == 3) {
                helper.setVisible(R.id.tv_submit, true);
            }
            //查看题目是否做过并且不是错题类型
            if (!TextUtils.isEmpty(practiceData.isDo()) && type != 7) {
                answer =Integer.valueOf(practiceData.getDoA());
                if (practiceData.getOptionType() == 3) {
                    answerDouble(helper, practiceData,Integer.valueOf(practiceData.getDoA()), Integer.valueOf(practiceData.isDo()),helper.getAdapterPosition());
                    return;
                }
                if (Integer.valueOf(practiceData.isDo()) == 1) {
                    answer(true, getOptionImageView(helper), getTextView(helper), helper, practiceData, -1,helper.getAdapterPosition());
                } else {
                    answer(false, getOptionImageView(helper), getTextView(helper), helper, practiceData, -1,helper.getAdapterPosition());
                }
            }
        } else {
            helper.setVisible(R.id.tv_difficulty, false);
            helper.setText(R.id.tv_difficulty, "难度系数: " + practiceData.getDifficulty());
            helper.setVisible(R.id.ll_practice_explain, true);
            if (practiceData.getOptionType() == 3) {
                answerDouble(helper, practiceData);
                return;
            }
            answer = practiceData.getAnswer();
            answer(helper, practiceData);
        }
    }

    private void isShow(String content, View view) {
        if (TextUtils.isEmpty(content)) {
            view.setVisibility(View.GONE);
        } else {
            view.setVisibility(View.VISIBLE);
        }
    }

    private void answer(boolean isRight, OptionImageView view, TextView textView, BaseViewHolder helper, PracticeData practiceData, int myAnswer,int position) {
        if (isRight) {
            view.setRightState();
            textView.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
            if (myAnswer != -1) {
                practiceData.setDoIt(1);
                EventBus.getDefault().post(new PracticeEvent(true, practiceData.getId(), myAnswer,position));
            }
        } else {
            helper.setVisible(R.id.ll_practice_explain, true);
            view.setWrongState();
            textView.setTextColor(ContextCompat.getColor(mContext, R.color.textColorWrong));
            if (practiceData.getAnswer() == 1) {
                helper.setText(R.id.tv_explain_question, "答案：A");
            } else if (practiceData.getAnswer() == 2) {
                helper.setText(R.id.tv_explain_question, "答案：B");
            } else if (practiceData.getAnswer() == 3) {
                helper.setText(R.id.tv_explain_question, "答案：C");
            } else {
                helper.setText(R.id.tv_explain_question, "答案：D");
            }
            helper.setText(R.id.tv_explain_info, practiceData.getExplain());
            if (myAnswer != -1) {
                practiceData.setDoIt(2);
                EventBus.getDefault().post(new PracticeEvent(false, practiceData.getId(), myAnswer,position));
            }
        }
        helper.getView(R.id.rl_option_a).setEnabled(false);
        helper.getView(R.id.rl_option_b).setEnabled(false);
        helper.getView(R.id.rl_option_c).setEnabled(false);
        helper.getView(R.id.rl_option_d).setEnabled(false);
    }

    private TextView getTextView(BaseViewHolder helper) {
        TextView textView = null;
        if (answer == 1) {
            textView = helper.getView(R.id.tv_option_a);
        } else if (answer == 2) {
            textView = helper.getView(R.id.tv_option_b);
        } else if (answer == 3) {
            textView = helper.getView(R.id.tv_option_c);
        } else if (answer == 4) {
            textView = helper.getView(R.id.tv_option_d);
        }
        return textView;
    }

    private OptionImageView getOptionImageView(BaseViewHolder helper) {
        if (answer == 1) {
            return helper.getView(R.id.img_option_a);
        } else if (answer == 2) {
            return helper.getView(R.id.img_option_b);
        } else if (answer == 3) {
            return helper.getView(R.id.img_option_c);
        } else if (answer == 4) {
            return helper.getView(R.id.img_option_d);
        }
        return null;
    }


    private void answer(BaseViewHolder helper, PracticeData practiceData) {
        OptionImageView view = getOptionImageView(helper);
        TextView textView = getTextView(helper);
        view.setRightState();
        textView.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));

        if (practiceData.getAnswer() == 1) {
            helper.setText(R.id.tv_explain_question, "答案：A");
        } else if (practiceData.getAnswer() == 2) {
            helper.setText(R.id.tv_explain_question, "答案：B");
        } else if (practiceData.getAnswer() == 3) {
            helper.setText(R.id.tv_explain_question, "答案：C");
        } else {
            helper.setText(R.id.tv_explain_question, "答案：D");
        }
        helper.setText(R.id.tv_explain_info, practiceData.getExplain());

        helper.getView(R.id.rl_option_a).setEnabled(false);
        helper.getView(R.id.rl_option_b).setEnabled(false);
        helper.getView(R.id.rl_option_c).setEnabled(false);
        helper.getView(R.id.rl_option_d).setEnabled(false);
    }

    private void answerDouble(BaseViewHolder helper, PracticeData practiceData, Map<Integer, Boolean> map, boolean isNotice,int position) {
        helper.setVisible(R.id.tv_submit, false);
        String myAnswer = "";
        Set<Map.Entry<Integer, Boolean>> set = map.entrySet();
        for (Map.Entry<Integer, Boolean> me : set) {
            if (me.getValue()) {
                myAnswer += me.getKey();
            }
        }

        if (myAnswer.length() == String.valueOf(practiceData.getAnswer()).length()) {
            int a = 0;
            for (int i = 0; i < myAnswer.length(); i++) {
                a += myAnswer.charAt(i);
            }
            int b = 0;
            for (int i = 0; i < String.valueOf(practiceData.getAnswer()).length(); i++) {
                b += String.valueOf(practiceData.getAnswer()).charAt(i);
            }
            if (a == b) {
                //答对
                if (map.containsKey(1)) {
                    ((OptionImageView) helper.getView(R.id.img_option_a)).setRightState();
                    ((TextView) helper.getView(R.id.tv_option_a)).setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                }
                if (map.containsKey(2)) {
                    ((OptionImageView) helper.getView(R.id.img_option_b)).setRightState();
                    ((TextView) helper.getView(R.id.tv_option_b)).setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                }
                if (map.containsKey(3)) {
                    ((OptionImageView) helper.getView(R.id.img_option_c)).setRightState();
                    ((TextView) helper.getView(R.id.tv_option_c)).setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                }
                if (map.containsKey(4)) {
                    ((OptionImageView) helper.getView(R.id.img_option_d)).setRightState();
                    ((TextView) helper.getView(R.id.tv_option_d)).setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                }
                helper.getView(R.id.rl_option_a).setEnabled(false);
                helper.getView(R.id.rl_option_b).setEnabled(false);
                helper.getView(R.id.rl_option_c).setEnabled(false);
                helper.getView(R.id.rl_option_d).setEnabled(false);
                helper.getView(R.id.tv_submit).setEnabled(false);
                if (isNotice) {
                    practiceData.setDoIt(1);
                    EventBus.getDefault().post(new PracticeEvent(true, practiceData.getId(), Integer.valueOf(myAnswer),position));
                }
                return;
            }
        }
        //答错
        String explain = "";
        if (map.containsKey(1)) {
            if (String.valueOf(practiceData.getAnswer()).indexOf("1") != -1) {
                explain = "A";
                ((OptionImageView) helper.getView(R.id.img_option_a)).setRightState();
                ((TextView) helper.getView(R.id.tv_option_a)).setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
            } else {
                ((OptionImageView) helper.getView(R.id.img_option_a)).setWrongState();
                ((TextView) helper.getView(R.id.tv_option_a)).setTextColor(ContextCompat.getColor(mContext, R.color.textColorWrong));
            }
        } else if (String.valueOf(practiceData.getAnswer()).indexOf("1") != -1) {
            explain = "A";
        }

        if (map.containsKey(2)) {
            if (String.valueOf(practiceData.getAnswer()).indexOf("2") != -1) {
                explain += "B";
                ((OptionImageView) helper.getView(R.id.img_option_b)).setRightState();
                ((TextView) helper.getView(R.id.tv_option_b)).setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
            } else {
                ((OptionImageView) helper.getView(R.id.img_option_b)).setWrongState();
                ((TextView) helper.getView(R.id.tv_option_b)).setTextColor(ContextCompat.getColor(mContext, R.color.textColorWrong));
            }
        } else if (String.valueOf(practiceData.getAnswer()).indexOf("2") != -1) {
            explain += "B";
        }

        if (map.containsKey(3)) {
            if (String.valueOf(practiceData.getAnswer()).indexOf("3") != -1) {
                explain += "C";
                ((OptionImageView) helper.getView(R.id.img_option_c)).setRightState();
                ((TextView) helper.getView(R.id.tv_option_c)).setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
            } else {
                ((OptionImageView) helper.getView(R.id.img_option_c)).setWrongState();
                ((TextView) helper.getView(R.id.tv_option_c)).setTextColor(ContextCompat.getColor(mContext, R.color.textColorWrong));
            }
        } else if (String.valueOf(practiceData.getAnswer()).indexOf("3") != -1) {
            explain += "C";
        }

        if (map.containsKey(4)) {
            if (String.valueOf(practiceData.getAnswer()).indexOf("4") != -1) {
                explain += "D";
                ((OptionImageView) helper.getView(R.id.img_option_d)).setRightState();
                ((TextView) helper.getView(R.id.tv_option_d)).setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
            } else {
                ((OptionImageView) helper.getView(R.id.img_option_d)).setWrongState();
                ((TextView) helper.getView(R.id.tv_option_d)).setTextColor(ContextCompat.getColor(mContext, R.color.textColorWrong));
            }
        } else if (String.valueOf(practiceData.getAnswer()).indexOf("4") != -1) {
            explain += "D";
        }
        helper.setVisible(R.id.ll_practice_explain, true);
        helper.setText(R.id.tv_explain_question, "答案：" + explain);
        helper.setText(R.id.tv_explain_info, practiceData.getExplain());
        helper.getView(R.id.rl_option_a).setEnabled(false);
        helper.getView(R.id.rl_option_b).setEnabled(false);
        helper.getView(R.id.rl_option_c).setEnabled(false);
        helper.getView(R.id.rl_option_d).setEnabled(false);
        helper.getView(R.id.tv_submit).setEnabled(false);
        if (isNotice) {
            practiceData.setDoIt(2);
            EventBus.getDefault().post(new PracticeEvent(false, practiceData.getId(), Integer.valueOf(myAnswer),position));
        }
    }

    private void answerDouble(BaseViewHolder helper, PracticeData practiceData) {
        String explain = "";
        if (String.valueOf(practiceData.getAnswer()).indexOf("1") != -1) {
            explain = "A";
            ((OptionImageView) helper.getView(R.id.img_option_a)).setRightState();
            ((TextView) helper.getView(R.id.tv_option_a)).setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
        } else {
            ((OptionImageView) helper.getView(R.id.img_option_a)).setNormalState();
            ((TextView) helper.getView(R.id.tv_option_a)).setTextColor(ContextCompat.getColor(mContext, R.color.textColorWrong));
        }

        if (String.valueOf(practiceData.getAnswer()).indexOf("2") != -1) {
            explain += "B";
            ((OptionImageView) helper.getView(R.id.img_option_b)).setRightState();
            ((TextView) helper.getView(R.id.tv_option_b)).setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
        } else {
            ((OptionImageView) helper.getView(R.id.img_option_b)).setNormalState();
            ((TextView) helper.getView(R.id.tv_option_b)).setTextColor(ContextCompat.getColor(mContext, R.color.textColorWrong));
        }

        if (String.valueOf(practiceData.getAnswer()).indexOf("3") != -1) {
            explain += "C";
            ((OptionImageView) helper.getView(R.id.img_option_c)).setRightState();
            ((TextView) helper.getView(R.id.tv_option_c)).setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
        } else {
            ((OptionImageView) helper.getView(R.id.img_option_c)).setNormalState();
            ((TextView) helper.getView(R.id.tv_option_c)).setTextColor(ContextCompat.getColor(mContext, R.color.textColorWrong));
        }

        if (String.valueOf(practiceData.getAnswer()).indexOf("4") != -1) {
            explain += "D";
            ((OptionImageView) helper.getView(R.id.img_option_d)).setRightState();
            ((TextView) helper.getView(R.id.tv_option_d)).setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
        } else {
            ((OptionImageView) helper.getView(R.id.img_option_d)).setNormalState();
            ((TextView) helper.getView(R.id.tv_option_d)).setTextColor(ContextCompat.getColor(mContext, R.color.textColorWrong));
        }

        helper.setVisible(R.id.ll_practice_explain, true);
        helper.setText(R.id.tv_explain_question, "答案：" + explain);
        helper.setText(R.id.tv_explain_info, practiceData.getExplain());
        helper.getView(R.id.rl_option_a).setEnabled(false);
        helper.getView(R.id.rl_option_b).setEnabled(false);
        helper.getView(R.id.rl_option_c).setEnabled(false);
        helper.getView(R.id.rl_option_d).setEnabled(false);
    }

    private void answerDouble(BaseViewHolder helper, PracticeData practiceData, int myAnswer, int type,int position) {
        helper.setVisible(R.id.tv_submit, false);
        if (type == 1) {
            answerDouble(helper, practiceData);
            return;
        }
        Map<Integer, Boolean> map = new HashMap<>();
        if (String.valueOf(myAnswer).indexOf("1") != -1) {
            map.put(1, true);
        }

        if (String.valueOf(myAnswer).indexOf("2") != -1) {
            map.put(2, true);
        }

        if (String.valueOf(myAnswer).indexOf("3") != -1) {
            map.put(3, true);
        }

        if (String.valueOf(myAnswer).indexOf("4") != -1) {
            map.put(4, true);
        }
        answerDouble(helper, practiceData, map, false,position);
    }

    public void setMode(int mode) {
        this.mode = mode;
    }
}
